package lab03.test12;

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = null;
        Statement statement = null;

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java-2017?serverTimezone=UTC", "root", "193749");
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM t_users");
            System.out.println("查询全部记录.");
            System.out.println("id" + " usename" + " password" + " register_datetime");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + "   ");
                System.out.print(resultSet.getString(2) + "   ");
                System.out.print(resultSet.getString(3) + "   ");
                System.out.println(resultSet.getString(4));
            }//查询所有记录

            System.out.println();
            System.out.println("查询用户名为admin的记录.");
            resultSet = statement.executeQuery("SELECT * FROM t_users WHERE usename = 'admin'");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + "    ");
                System.out.print(resultSet.getString(2) + "   ");
                System.out.print(resultSet.getString(3) + "   ");
                System.out.println(resultSet.getString(4));
            }//查询所有记录

            System.out.println();
            System.out.println("查询用户名为admin的记录,密码为123456的记录.");
            resultSet = statement.executeQuery("SELECT * FROM t_users WHERE usename = 'chen' AND password = '123456'");

            System.out.println();
            System.out.println("；查询注册时间大于10000000的记录；");
            resultSet = statement.executeQuery("SELECT * FROM t_users WHERE register_datetime > '10000000'");
            System.out.println("id" + " usename" + " password" + " register_datetime");
            while (resultSet.next()) {
                System.out.print(resultSet.getString(1) + "  ");
                System.out.print(resultSet.getString(2) + "  ");
                System.out.print(resultSet.getString(3) + "  ");
                System.out.println(resultSet.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
